package com.jrx.anytxn.param.controller.rights;

import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.dto.operation.PrPriceLabelReq;
import com.jrx.anytxn.param.dto.operation.PrPriceLabelRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.rights.IPrPriceLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 张靖烽
 * @name PrPriceLabelController
 * @description 定价标签表管理
 * @create 2020-12-08 16:14
 **/
@Api(tags = "定价标签表相关页面api")
@RestController
@RequestMapping("/param")
public class PrPriceLabelController {

    private static final Logger logger = LoggerFactory.getLogger(PrPriceLabelController.class);

    @Autowired
    private IPrPriceLabelService iPrPriceLabelService;
    @Autowired
    private InfoService infoService;

    @ApiOperation("根据租户ID分页查询定价标签信息")
    @GetMapping("/priceLabel/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<PrPriceLabelRes>> getAllPrPriceLabelsByPage(@PathVariable(value = "pageNum") Integer pageNum,
                                                                             @PathVariable(value = "pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<PrPriceLabelRes>> result = new TxnRespResult<>();
        TxnPage<PrPriceLabelRes> res;
        try {
            res = iPrPriceLabelService.getAllPriceLabelByTenId(pageNum, pageSize, String.valueOf(infoService.getUser().getProjectId()));
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getErrMsg(), e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation("通过标签代码和租户Id查询信息")
    @GetMapping("/priceLabel/labelCode/{labelCode}")
    @ApiImplicitParams({@ApiImplicitParam(name = "labelCode", value = "标签代码", required = true)})
    public TxnRespResult<PrPriceLabelRes> getByLabelCodeAndTenId(@PathVariable(value = "labelCode") String labelCode) {
        TxnRespResult<PrPriceLabelRes> result = new TxnRespResult<>();
        PrPriceLabelRes res;
        try {
            res = iPrPriceLabelService.getByLabelCodeAndTenId(labelCode, String.valueOf(infoService.getUser().getProjectId()));
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getErrMsg(), e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation("增加定价标签信息")
    @PostMapping("/priceLabel")
    public TxnRespResult<PrPriceLabelRes> createPriceLabel(@RequestBody PrPriceLabelReq prPriceLabelReq) {
        TxnRespResult<PrPriceLabelRes> result = new TxnRespResult<>();
        PrPriceLabelRes res;
        try {
            prPriceLabelReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            prPriceLabelReq.setUpdateBy(infoService.getUser().getUserName());
            res = iPrPriceLabelService.addPriceLabel(prPriceLabelReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getErrMsg(), e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation("修改定价标签信息")
    @PutMapping("/priceLabel")
    public TxnRespResult<PrPriceLabelRes> modifyPriceLabel(@RequestBody PrPriceLabelReq prPriceLabelReq) {
        TxnRespResult<PrPriceLabelRes> result = new TxnRespResult<>();
        PrPriceLabelRes res;
        prPriceLabelReq.setUpdateBy(infoService.getUser().getUserName());
        res = iPrPriceLabelService.modifyPriceLabel(prPriceLabelReq);
        result.getSuccess(res);
        return result;
    }

    @ApiOperation("根据租户ID和标签代码删除定价标签信息")
    @DeleteMapping("/priceLabel/{labelCode}")
    @ApiImplicitParams({@ApiImplicitParam(name = "labelCode", value = "标签代码", required = true)})
    public TxnRespResult<Boolean> removeById(@PathVariable("labelCode") String labelCode) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);
            flag = iPrPriceLabelService.removeByLabelCodeAndTenId(labelCode, String.valueOf(infoService.getUser().getProjectId()));
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getErrMsg(), e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }
}
